<template>
  <div class="message" :style="{backgroundImage:`url(${$store.state.UrlBase +'/frontUploads/messageImage.jpg' })`}">
    <home-header />
    <div class="addMeassage">
      <h5>留言板</h5>
      <input placeholder="来了就说点什么" type="text" class="input" required="" v-model="message" @keyup.enter="addMessage" />
      <button @click="addMessage">
        发送
      </button>
    </div>
    <vue-danmaku v-model:danmus="danmus" :loop='true' :fontSize="25" :channels='0' ref="danmakuRef" :isSuspend="true"
      :speeds="250" :randomChannel="true" style="height:100%;width:100%; color:#fff">
    </vue-danmaku>
  </div>
</template>
<script>
// 弹幕
import vueDanmaku from 'vue3-danmaku'
import { ElMessage } from 'element-plus'
import { reactive, toRefs, ref, onMounted } from 'vue'
import HomeHeader from '@/components/home/HomeHeader.vue'
import http from '@/utils/http'
export default {
  components: { HomeHeader, vueDanmaku },
  name: 'MessagePage',
  setup (props) {
    const data = reactive({
      message: '',
      danmus: []
    })
    const danmakuRef = ref(null)
    onMounted(() => {
      danmakuRef.value.show()
      http.get('/message?page=1&limit=200').then(res => {
        data.danmus.push(...res.data.inof.data.map(item => item.message))
        danmakuRef.value.play()

      })
    })
    function addMessage () {
      if (!data.message.trim()) {
        ElMessage({
          showClose: true,
          message: '不能为空',
          type: 'error',
        })
        return
      }
      http.post('/message', { message: data.message }).then(res => {
        data.danmus.push(data.message)
        ElMessage({
          message: '添加成功',
          type: 'success',
        })
        data.message = ''
      })
    }

    return {
      ...toRefs(data),
      addMessage,
      danmakuRef
    }
  }
}
</script>
<style>
.dm {
  color: #fff !important;
}
</style>
<style lang="scss" scoped>
.message {
  position: absolute;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-color: #2b2b2b;
}
.addMeassage {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 100;
  transform: translate(-50%, -50%);
  h5 {
    position: absolute;
    width: 100%;
    top: -150px;
    color: #ddd;
    font-size: 35px;
    text-align: center;
    letter-spacing: 5px;
    padding-left: 50px;
  }
  button {
    position: absolute;
    right: -70px;
    top: 10px;
    width: 60px;
    background-color: #f2f2f2;
    color: #888;
    border-radius: 10em;
    font-size: 12px;
    font-weight: 600;
    padding: 5px 8px;
    cursor: pointer;
    transition: all 0.1s ease-in-out;
    border: 1px solid #888;
    transform: translateY(-4px) translateX(-2px);
    box-shadow: 2px 5px 0 0 black;
  }

  button:active {
    transform: translateY(3px) translateX(3px);
    box-shadow: 0 0 black;
  }
}
/* From uiverse.io by @alexruix */
.input {
  width: 200px;
  line-height: 28px;
  border: 2px solid transparent;
  border-bottom-color: #fff;
  padding: 0.2rem 0;
  outline: none;
  background-color: transparent;
  color: #fff;
  transition: 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.input:focus,
input:hover {
  outline: none;
  padding: 0.2rem 1rem;
  border-radius: 1rem;
  border-color: #7a9cc6;
}

.input::placeholder {
  color: #f2f2f2;
  // font-size: 16px;
}

.input:focus::placeholder {
  opacity: 0;
  transition: opacity 0.3s;
}
</style>